package com.cs.controller;

import com.cs.domain.CsApplications;
import com.cs.domain.CsCommittee;
import com.cs.domain.CsSupplier;
import com.cs.domain.vo.ApplicationVO;
import com.cs.domain.vo.ExpertEducateVO;
import com.cs.service.ICsApplicationsService;
import com.ruoyi.common.core.domain.R;
import com.ruoyi.common.core.utils.poi.ExcelUtil;
import com.ruoyi.common.core.web.controller.BaseController;
import com.ruoyi.common.core.web.domain.AjaxResult;
import com.ruoyi.common.core.web.page.TableDataInfo;
import com.ruoyi.common.log.annotation.Log;
import com.ruoyi.common.log.enums.BusinessType;
import com.ruoyi.common.security.annotation.RequiresLogin;
import com.ruoyi.common.security.annotation.RequiresPermissions;
import com.ruoyi.common.security.annotation.RequiresRoles;
import com.ruoyi.system.api.RemoteUserService;
import com.ruoyi.system.api.domain.SysUser;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletResponse;
import java.util.List;

/**
 * 抽取申请Controller
 *
 * @author ruoyi
 * @date 2024-12-03
 */
@RestController
@RequestMapping("/applications")
public class CsApplicationsController extends BaseController {
    @Autowired
    private ICsApplicationsService csApplicationsService;

    //关联ry-config表与中间表
    @PostMapping("/register")
    @Transactional
    public R<Boolean> registerUserInfo(@RequestBody SysUser sysUser) {
        CsSupplier csSupplier = new CsSupplier();
        csSupplier.sethName(sysUser.getUserName());
        csSupplier.sethLoginaccount(sysUser.getUserName());
        csSupplier.sethPassword(sysUser.getPassword());
        Integer res = csApplicationsService.addSupplier(csSupplier);
        if (res != 0) {
            csApplicationsService.addRelation(csSupplier.getHid(), sysUser.getUserId());
        }
        return R.ok(true);
    }

    /**
     * 查询抽取申请列表
     */
    @GetMapping("/list")
    public TableDataInfo list(CsApplications csApplications) {
        startPage();
        List<CsApplications> list = csApplicationsService.selectCsApplicationsList(csApplications);
        return getDataTable(list);
    }

    /**
     * 导出抽取申请列表
     */
    @RequiresPermissions("system:applications:export")
    @Log(title = "抽取申请", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(HttpServletResponse response, CsApplications csApplications) {
        List<CsApplications> list = csApplicationsService.selectCsApplicationsList(csApplications);
        ExcelUtil<CsApplications> util = new ExcelUtil<CsApplications>(CsApplications.class);
        util.exportExcel(response, list, "抽取申请数据");
    }

    /**
     * 获取抽取申请详细信息
     */
    @RequiresPermissions("system:applications:query")
    @GetMapping(value = "/{xid}")
    public AjaxResult getInfo(@PathVariable("xid") Long xid) {
        return success(csApplicationsService.selectCsApplicationsByXid(xid));
    }

    /**
     * 新增抽取申请
     */
    @RequiresPermissions("system:applications:add")
    @Log(title = "抽取申请", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody CsApplications csApplications) {
        return toAjax(csApplicationsService.insertCsApplications(csApplications));
    }

    /**
     * 修改抽取申请
     */
    @RequiresPermissions("system:applications:edit")
    @Log(title = "抽取申请", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody CsApplications csApplications) {
        return toAjax(csApplicationsService.updateCsApplications(csApplications));
    }

    /**
     * 删除抽取申请
     */
    @RequiresLogin
    @Log(title = "抽取申请", businessType = BusinessType.DELETE)
    @DeleteMapping("/{xids}")
    public AjaxResult remove(@PathVariable Long[] xids) {
        return toAjax(csApplicationsService.deleteCsApplicationsByXids(xids));
    }


    /**
     * 抽取专家
     *
     * @param applicationVO
     * @return
     */
    @RequiresRoles(value = {"admin"})
    @PostMapping("/addApplications")
    public R<List<ExpertEducateVO>> addApplications(@RequestBody ApplicationVO applicationVO) {
        return csApplicationsService.addApplications(applicationVO);
    }

    /**
     * 查询评标委员会
     */
    @GetMapping("/committeeList")
    public TableDataInfo committeeList(CsCommittee csCommittee) {
        startPage();
        List<CsCommittee> committeeList = csApplicationsService.committeeList(csCommittee);
        return getDataTable(committeeList);
    }


    /**
     * 删除评标委员会
     */
    @RequiresRoles(value = {"admin"})
    @DeleteMapping("/deleteCommittee/{jId}")
    public R<Integer> deleteCommittee(@PathVariable String jId) {
        return csApplicationsService.deleteCommittee(jId);
    }


    @GetMapping("/getStartTimeAndEndTime/{iNumber}")
    public R<CsApplications> getStartTimeAndEndTime(@PathVariable String iNumber){
        return csApplicationsService.getStartTimeAndEndTime(iNumber);
    }
}